<template>
  <div class="p-6px">
    <el-card class="rounded-md" shadow="never">
      <!-- 搜索条件 -->
      <el-form v-show="showSearch" :inline="true" id="searchFormId">
        <el-form-item label="登录名称" prop="loginName">
          <el-input
            placeholder="请输入登录名称"
            v-model="searchParams.loginName"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleListPage"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户名字" prop="userName">
          <el-input
            placeholder="请输入用户名字"
            v-model="searchParams.userName"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleListPage"
          ></el-input>
        </el-form-item>
        <el-form-item label="IP地址" prop="ipAddress">
          <el-input
            placeholder="请输入IP地址"
            v-model="searchParams.ipAddress"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleListPage"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" plain @click="handleSearch()" v-auth="['monitor:online:search']">搜索</el-button>
          <el-button type="danger" icon="refresh" plain @click="resetSearch()">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格头部按钮 -->
      <el-row :gutter="10">
        <KoiToolbar v-model:showSearch="showSearch" @refreshTable="handleListPage"></KoiToolbar>
      </el-row>

      <br />
      <!-- 数据表格 -->
      <el-table v-loading="loading" v-adaptive border :data="tableList" empty-text="暂时没有数据哟🌻">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户ID" prop="userId" width="200px" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column
          label="登录名称"
          prop="loginName"
          width="130px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="用户名字"
          prop="userName"
          width="150px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="部门名称"
          prop="deptName"
          width="150px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="IP地址"
          prop="ipAddress"
          width="180px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="登录地址"
          prop="loginAddress"
          width="180px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="浏览器"
          prop="browser"
          width="180px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="操作系统"
          prop="os"
          width="180px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="登录设备"
          prop="deviceName"
          width="120px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="登录时间"
          prop="loginTime"
          width="180px"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right" v-auth="['monitor:online:kickout', 'monitor:online:logout']">
          <template #default="{ row }">
            <el-tooltip content="强退🌻" placement="top">
              <el-button
                type="danger"
                icon="Remove"
                circle
                plain
                @click="handleKickOut(row)"
                v-auth="['monitor:online:kickout']"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="注销🌻" placement="top">
              <el-button
                type="danger"
                icon="CircleClose"
                circle
                plain
                @click="handleLogout(row)"
                v-auth="['monitor:online:logout']"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <br />
      <!-- {{ searchParams.pageNo }} --- {{ searchParams.pageSize }} -->
      <!-- 分页 -->
      <el-pagination
        background
        v-model:current-page="searchParams.pageNo"
        v-model:page-size="searchParams.pageSize"
        v-show="total > 0"
        :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleListPage"
        @current-change="handleListPage"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts" name="onlinePage">
import { ref, onMounted } from "vue";
import { koiNoticeSuccess, koiNoticeError, koiMsgWarning, koiMsgError, koiMsgBox } from "@/utils/koi.ts";
import { listPage, kickOut, logout } from "@/api/monitor/online/index.ts";

// 数据表格加载页面动画
const loading = ref(false);
/** 是否显示搜索表单 */
const showSearch = ref<boolean>(true); // 默认显示搜索条件
// 数据表格数据
const tableList = ref<any>([]);

// 查询参数
const searchParams = ref({
  pageNo: 1, // 第几页
  pageSize: 10, // 每页显示多少条
  loginName: "",
  userName: "",
  ipAddress: ""
});
const total = ref<number>(0);
// 重置搜索参数
const resetSearchParams = () => {
  searchParams.value = {
    pageNo: 1,
    pageSize: 10,
    loginName: "",
    userName: "",
    ipAddress: ""
  };
};

/** 搜索 */
const handleSearch = () => {
  console.log("搜索");
  searchParams.value.pageNo = 1;
  handleTableData();
};

/** 重置 */
const resetSearch = () => {
  console.log("重置搜索");
  resetSearchParams();
  handleListPage();
};

/** @current-change：点击分页组件页码发生变化：例如：切换第2、3页 OR 上一页 AND 下一页 OR 跳转某一页 */
/** @size-change：点击分页组件下拉选中条数发生变化：例如：选择10条/页、20条/页等 */
// 分页查询，@current-change AND @size-change都会触发分页，调用后端分页接口
/** 数据表格 */
const handleListPage = async () => {
  try {
    tableList.value = []; // 重置表格数据
    loading.value = true;
    const res: any = await listPage(searchParams.value);
    console.log("在线用户数据表格数据->", res.data);
    tableList.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  } catch (error) {
    console.log(error);
    koiNoticeError("数据查询失败，请刷新重试🌻");
  }
};

/** 数据表格[不带Loading，删除、批量删除等使用] */
const handleTableData = async () => {
  try {
    const res: any = await listPage(searchParams.value);
    console.log("在线用户数据表格数据->", res.data);
    tableList.value = res.data.records;
    total.value = res.data.total;
  } catch (error) {
    console.log(error);
    koiNoticeError("数据查询失败，请刷新重试🌻");
  }
};

onMounted(() => {
  // 获取数据表格数据
  handleListPage();
});

/** 强退 */
const handleKickOut = (row: any) => {
  const id = row.userId;
  if (id == null || id == "") {
    koiMsgWarning("请选中需要强退的数据🌻");
  }
  koiMsgBox("您确认强退登录名称[" + row.loginName + "]么？")
    .then(async () => {
      try {
        await kickOut(id);
        handleTableData();
        koiNoticeSuccess("强退成功🌻");
      } catch (error) {
        console.log(error);
        handleTableData();
        koiNoticeError("强退失败，请刷新重试🌻");
      }
    })
    .catch(() => {
      koiMsgError("已取消🌻");
    });
}

/** 注销 */
const handleLogout = (row: any) => {
  const id = row.userId;
  if (id == null || id == "") {
    koiMsgWarning("请选中需要注销的数据🌻");
  }
  koiMsgBox("您确认注销登录名称[" + row.loginName + "]么？")
    .then(async () => {
      try {
        await logout(id);
        handleTableData();
        koiNoticeSuccess("注销成功🌻");
      } catch (error) {
        console.log(error);
        handleTableData();
        koiNoticeError("注销失败，请刷新重试🌻");
      }
    })
    .catch(() => {
      koiMsgError("已取消🌻");
    });
}
</script>

<style lang="scss" scoped></style>
